package com.qipay.admin.withdraw.service;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 代付的逻辑
 * 鉴权绑卡那三个接口是需要接的：    绑卡请求  绑卡确认  和短验重发
 * 绑卡支付接口
 * 回调
 */
@Service
public class KoukuanService {
    private static Logger logger = LoggerFactory.getLogger(KoukuanService.class);


    private String charset = "utf-8";
    /**
     * 绑卡请求
     * @return
     */
    @Transactional
    public Map<String,Object> bindCard(){
        String api_url="/rest/v1.0/paperorder/unified/auth/request";
        Map<String, Object> params = new HashMap<>();
        params.put("requestno","123456");//	商户生成的唯一绑卡请求号
        params.put("identityid","100001");//商户生成的用户唯一标识。
        params.put("identitytype","PHONE");//	用户标识类型
        params.put("advicesmstype","MESSAGE");//建议短验发送类型
        params.put("callbackurl","localhost:8080");
        params.put("issms","false");//true:有短验 false:无短验
        params.put("cardno","6217851700000827401");
        params.put("idcardno","420621199107278314");
        params.put("idcardtype","ID");
        params.put("username","付俊");
        params.put("phone","17859712056");
        params.put("remark","测试");
        params.put("authtype","COMMON_FOUR");
        params.put("extinfos","{}");
        params.put("","");





        Map<String, Object> responseMap = null;

        return responseMap;
    }

    /**
     * 绑卡确认
     * @return
     */
    @Transactional
    public Map<String,Object> sureBind(){
        String api_url="/rest/v1.0/paperorder/auth/confirm";
        Map<String, Object> responseMap = null;

        return responseMap;
    }

    /**
     * 绑卡短息重发
     * @return
     */
    @Transactional
    public Map<String,Object> sendMsgAgain(){


        Map<String, Object> responseMap = null;

        return responseMap;
    }

    /**
     * 绑卡支付
     * @return
     */
    @Transactional
    public Map<String,Object> pay(){
        String api_url="/rest/v1.0/paperorder/unified/pay";

        Map<String, Object> responseMap = null;

        return responseMap;
    }

    /**
     * 回调通知
     * @return
     */
    @Transactional
    public void recNotify(){
        Map<String, Object> responseMap = null;

    }




    protected String sendPostForm(Map<String, Object> params, String url) {
        final HttpParams httpParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParams, 30000);
        HttpConnectionParams.setSoTimeout(httpParams,30000);
        HttpClient client = new DefaultHttpClient(httpParams);

        String result = null;

        HttpPost post = new HttpPost(url);
        try {
            if (null != params) {
                logger.info(getClass().getName() +
                        " pay sendPost request body : " +
                        params + ", request url:" + url);
                List<NameValuePair> pairList = new ArrayList<NameValuePair>(params.size());
                for (Map.Entry<String, Object> entry : params.entrySet()) {
                    NameValuePair pair = new BasicNameValuePair(entry.getKey(), entry
                            .getValue().toString());
                    pairList.add(pair);
                }
                post.setEntity(new UrlEncodedFormEntity(pairList, Charset.forName("UTF-8")));
            }

            HttpResponse resultResp = client.execute(post);

            if (resultResp.getStatusLine().getStatusCode() == 200) {
                try {
                    result = EntityUtils.toString(resultResp.getEntity(), "utf8");
                    logger.info(getClass().getName() +
                            "pay sendPost response body : " + result + ", request url:" + url);
                } catch (Exception e) {
                    logger.error(e.toString());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.toString());
        }
        return result;
    }

}
